package com.mxprojects.views
{
	import com.mxprojects.admin.AdminConfiguration;
	import com.mxprojects.admin.AdminForums;
	import com.mxprojects.admin.AdminGroups;
	import com.mxprojects.admin.AdminHandleData;
	import com.mxprojects.admin.AdminUsers;
	import com.mxprojects.constructors.Dropdown;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.constructors.PopupWindow;
	import com.mxprojects.constructors.PopupWindowNav;
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.gui.BreadCrumbs;
	import com.mxprojects.gui.PageNav;
	import com.mxprojects.gui.Resizer;
	import com.mxprojects.gui.Styles;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	public class Admin
	{
		protected static const NAV_ELEMENTS:Array = new Array(Word.FORUMS, Word.GROUPS, Word.USERS, Word.CONFIGURATION);
		protected static const ADMIN_SCRIPT:String = "admin.php";
		protected static const DESCRIPTION_ROWS:int = 3;
		protected static const HOLDER_GAP:int = 30;
		
		protected static var popup:PopupWindow;
		protected static var popupNav:PopupWindowNav;
		protected static var baseHolder:Sprite;
		protected static var leftHolder:Sprite;
		protected static var rightHolder:Sprite;
		protected static var dataToSend:Object;
		protected static var dataFromServer:Object;
		protected static var adminTitle:SimpleTextField;
		protected static var serverHandler:ServerHandler = new ServerHandler();
		protected static var adminNav:SimpleTextField;
		
		public static function load():void {
			//see if we are able to be here
			changeLocation(NAV_ELEMENTS[0]);
			
			//clear the forum			
			MXP3.clearBoard();
			
			//resize the page if we need to
			Resizer.resizeContent();
		}
		
		private static function showAdmin(event:CustomEvent):void {
			//this will make sure they are allowed to be here
			dataFromServer = event.getServerData();
			
			//tell the board where we are
			MXP3.currentLocation = Word.ADMIN;
			
			//let's see what we need to do
			switch(dataFromServer.output){				
				case Word.FORUMS:
					AdminForums.display();
					break;
				
				case Word.GROUPS:
					AdminGroups.display();
					break;
				
				case Word.CONFIGURATION:
					AdminConfiguration.display();
					break;
				
				case Word.USERS:
					AdminUsers.display();
					break;
				
				default:
					setAdminTitle("showAdmin Default");
					break;
			}
			
			//move the holder to where it needs to be
			baseHolder.y = adminTitle.y + adminTitle.height + Config.TEXT_PADDING;
			baseHolder.x = adminTitle.x;
			
			//show the nav
			if(PageNav.navText.text == ""){
				buildNav();
			}
			
			//resize if we need to
			Resizer.resizeContent();
		}
		
		private static function buildNav():void {
			//takes the NAV_ELEMENTS and makes a text-based nav where the pages usually are
			for(var i:int = 0; i < NAV_ELEMENTS.length; i++){
				PageNav.navText.htmlText += "<a class=\"adminNav\" href=\"event:"+NAV_ELEMENTS[i]+"\">" + Language.main(NAV_ELEMENTS[i]) + "</a>  ";
			}
			
			//listen to the clicks
			if(!PageNav.navText.hasEventListener(TextEvent.LINK)){
				PageNav.navText.addEventListener(TextEvent.LINK, onNavClick, false, 0, true);
			}
		}
		
		private static function onNavClick(event:TextEvent):void {
			//load the other section
			changeLocation(event.text);
		}
		
		protected static function setAdminTitle(title:String):void {
			//allows you to change the title of the admin area
			var titleDisplay:String = Language.main(Word.ADMIN) + InputConstructor.LABEL_SEPARATOR + " " + title;
			var adminText:String = "<span class=\"adminTitle\">" + titleDisplay + "</span>";
			if(adminTitle == null){
				adminTitle = new SimpleTextField(adminText);
				adminTitle.x = Styles.css.getStyle(".adminTitle").x;
				adminTitle.y = Styles.css.getStyle(".adminTitle").y;
			}else{
				adminTitle.htmlText = adminText;
			}
			
			MXP3.addContent(adminTitle);
			
			//set the breadcrumbs and the title
			BreadCrumbs.createCrumbs(0, "", 0, "", Word.ADMIN, titleDisplay);
			DeepLinking.setTitle(titleDisplay);
		}
		
		protected static function changeLocation(location:String):void {			
			//prepare the data
			dataToSend = new Object();
			dataToSend.location = location;
				
			//load the stuff
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, showAdmin, null, ADMIN_SCRIPT);
		}
		
		protected static function buildGroupDropdown(groupDropdown:Dropdown, firstElement:String):Dropdown {			
			//this builds a selector based on all of the groups
			var groupDrop:Dropdown = groupDropdown;
			if(groupDrop == null){
				groupDrop = new Dropdown();
			}
			
			groupDrop.removeAll();
			
			//put the slect group in the dropdown
			groupDrop.addItem(firstElement, Dropdown.NULL_VALUE, false);
			
			//create the dropdown elements
			for(var i:int = 0; i < dataFromServer.groupTotal; i++){
				//if they have added some custom groups then we need to separate them
				if(i == 4){
					groupDrop.addItem(Language.main(Word.CUSTOM_GROUPS), Dropdown.NULL_VALUE, false);
				}
				
				//add the item to the dropdown
				groupDrop.addItem(dataFromServer["group" + i + "Name"], dataFromServer["group" + i + "ID"], false);
			}

			//update the dropdown before returning
			groupDrop.update();
			
			return groupDrop;
		}
		
		private static function close(event:MouseEvent = null):void {
			
		}
		
		protected static function setupHolders():void {
			//kill the holder if it's there
			removeHolder();
			
			//reset and add
			baseHolder = new Sprite();
			baseHolder.name = "holder";
			
			leftHolder = new Sprite();
			rightHolder = new Sprite();
			
			baseHolder.addChild(leftHolder);
			baseHolder.addChild(rightHolder);
			
			//add it to the stage
			MXP3.addContent(baseHolder);
		}
		
		protected static function removeHolder():void {
			//smoke it
			var contentHolder:Sprite = MXP3.getContent("holder") as Sprite;
			if(contentHolder != null){
				MXP3.contentHolder.removeChild(contentHolder);
			}
		}
		
		protected static function setLeftAndRight():void {
			//just a simple function to make sure the holders are at the proper gaps
			rightHolder.x = Math.round(leftHolder.width + HOLDER_GAP);
		}
		
		protected static function alignInputs(holder:Sprite, widestAmount:int):void {
			var childrenCount:int = holder.numChildren;
			
			for(var i:int = 0; i < childrenCount; i++){
				if(holder.getChildAt(i) is InputConstructor){
					//set the input box to the widestWidth
					var input:InputConstructor = holder.getChildAt(i) as InputConstructor;
					input.inputBox.x = widestAmount;
					input.inputBoxBackground.x = widestAmount;
				}
			}
		}

	}
}